/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#ifndef __NANO_TIMER_H__
#define __NANO_TIMER_H__
#include<time.h>
#include<limits.h>
class NanoTimer
{
    long long max_, min_, total, count, last_;
    struct timespec begin;
    struct timespec end;

public:
    NanoTimer() {  reset(); }
    inline void reset() {  max_ = 0; min_ = LONG_MAX; total =0; count = 0; last_ =0; }
    inline void start()
    {
        count++;
        clock_gettime(CLOCK_REALTIME, &begin);
    }
    inline void stop()
    {
        clock_gettime(CLOCK_REALTIME, &end);
        long long secs = end.tv_sec-begin.tv_sec;
        long long nano = end.tv_nsec-begin.tv_nsec;
        last_ = (secs*1000000000)+nano;
        total += last_;
        if ( max_ < last_ ) max_ = last_;
        if ( min_ > last_ ) min_ = last_;
    }
    inline long long last() { return last_; }
    inline long long avg() { return total/count; }
    inline long long min() { return min_; }
    inline long long max() { return max_; }
};

#endif /*__NANO_TIMER_H__*/
